package cn.exrick.xboot.modules.subsystemNews.controller;

import cn.exrick.xboot.common.constant.CommonConstant;
import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.subsystemNews.entity.SubsystemNews;
import cn.exrick.xboot.modules.subsystemNews.service.ISubsystemNewsService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author Exrick
 */
@Slf4j
@RestController
@Api(description = "子系统新闻管理接口")
@RequestMapping("/xboot/subsystemNews")
@Transactional

public class SubsystemNewsController {
    @Autowired
    private ISubsystemNewsService iSubsystemNewsService;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<SubsystemNews> get(@PathVariable String id){

        SubsystemNews subsystemNews = iSubsystemNewsService.getById(id);
        return new ResultUtil<SubsystemNews>().setData(subsystemNews);
    }

    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<SubsystemNews> getById(@Param("id") String id){

        SubsystemNews subsystemNews = iSubsystemNewsService.getById(id);
        return new ResultUtil<SubsystemNews>().setData(subsystemNews);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<SubsystemNews>> getAll(){

        List<SubsystemNews> list = iSubsystemNewsService.list();
        return new ResultUtil<List<SubsystemNews>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<SubsystemNews>> getByPage(@ModelAttribute PageVo page){

        IPage<SubsystemNews> data = iSubsystemNewsService.page(PageUtil.initMpPage(page));
        return new ResultUtil<IPage<SubsystemNews>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<SubsystemNews> saveOrUpdate(@ModelAttribute SubsystemNews subsystemNews){
        subsystemNews.setUpdateTime(new Date());

        System.out.println("id:"+subsystemNews.getId());
        if(iSubsystemNewsService.saveOrUpdate(subsystemNews)){
            return new ResultUtil<SubsystemNews>().setData(subsystemNews);
        }
        return new ResultUtil<SubsystemNews>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@PathVariable String[] ids){
       log.info("开始删除"+ids.length);
        for(String id : ids){
            System.out.println("id"+id);
            iSubsystemNewsService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }

        @RequestMapping(value = "/selectPageVo", method = RequestMethod.GET)
    @ApiOperation(value = "新闻条件分页获取数据")
    public Result<IPage<SubsystemNews>> selectPageVo(@ModelAttribute Page page, @Param("title") String title) {
        IPage<SubsystemNews> subsystemNewsIPage = iSubsystemNewsService.selectPageVo(page, title);
        return new ResultUtil<IPage<SubsystemNews>>().setData(subsystemNewsIPage);

    }

    @RequestMapping(value = "/selectPageVoMap", method = RequestMethod.GET)
    @ApiOperation(value = "新闻条件分页获取数据")
    public Result<IPage<SubsystemNews>> selectPageVoMap(@ModelAttribute Page page,@Param("source") String source, @Param("title") String title,@Param("department") String department) {
       // System.out.println("title:"+title+"department:"+department);
        Map<String, Object> map = new HashMap<String, Object>();
      /*  map.put("title","test");
        map.put("department","OA");*/
      if(department==null){
          System.out.println("department我是null");
      }
      if(title==null){
          System.out.println("title我是null");
      }
        if(title!=null&&!title.equals("")&&title.length()!=0&&!title.trim().isEmpty()){
            map.put("title",title);
        }
        if(department!=null&&!department.equals("")&&department.length()!=0&&!department.trim().isEmpty()){
            map.put("department", department);
        }
        if(source!=null&&!source.equals("")&&source.length()!=0&&!source.trim().isEmpty()){
            map.put("source", source);
        }
        IPage<SubsystemNews> subsystemNewsIPage = iSubsystemNewsService.selectPageVoMap(page,map);
        return new ResultUtil<IPage<SubsystemNews>>().setData(subsystemNewsIPage);
    }



    @RequestMapping(value = "/selectNoticePageVoMap", method = RequestMethod.GET)
    @ApiOperation(value = "公告条件分页获取数据")
    public Result<IPage<SubsystemNews>> selectNoticePageVoMap(@ModelAttribute Page page,@Param("source") String source, @Param("title") String title,@Param("department") String department) {

        Map<String, Object> map = new HashMap<String, Object>();

        if(title!=null&&!title.equals("")&&title.length()!=0&&!title.trim().isEmpty()){
            map.put("title",title);
        }
        if(department!=null&&!department.equals("")&&department.length()!=0&&!department.trim().isEmpty()){
            map.put("department", department);
        }
        if(source!=null&&!source.equals("")&&source.length()!=0&&!source.trim().isEmpty()){
            map.put("source", source);
        }
        IPage<SubsystemNews> subsystemNewsIPage = iSubsystemNewsService.selectNoticePageVoMap(page,map);
        return new ResultUtil<IPage<SubsystemNews>>().setData(subsystemNewsIPage);
    }


    @RequestMapping(value = "/selectPortalNoticePageVoMap", method = RequestMethod.GET)
    @ApiOperation(value = "portal公告条件分页获取数据")
    public Result<IPage<SubsystemNews>> selectPortalNoticePageVoMap(@ModelAttribute Page page,@Param("source") String source, @Param("title") String title,@Param("department") String department) {

        Map<String, Object> map = new HashMap<String, Object>();

        if(title!=null&&!title.equals("")&&title.length()!=0&&!title.trim().isEmpty()){
            map.put("title",title);
        }
        if(department!=null&&!department.equals("")&&department.length()!=0&&!department.trim().isEmpty()){
            map.put("department", department);
        }
        if(source!=null&&!source.equals("")&&source.length()!=0&&!source.trim().isEmpty()){
            map.put("source", source);
        }
        map.put("dictType", "portalNotice");
        IPage<SubsystemNews> subsystemNewsIPage = iSubsystemNewsService.selectPortalNoticePageVoMap(page,map);
        return new ResultUtil<IPage<SubsystemNews>>().setData(subsystemNewsIPage);
    }
    @RequestMapping(value = "/selectAppNoticePageVoMap", method = RequestMethod.GET)
    @ApiOperation(value = "app公告条件分页获取数据")
    public Result<IPage<SubsystemNews>> selectAppNoticePageVoMap(@ModelAttribute Page page,@Param("source") String source, @Param("title") String title,@Param("department") String department) {

        Map<String, Object> map = new HashMap<String, Object>();

        if(title!=null&&!title.equals("")&&title.length()!=0&&!title.trim().isEmpty()){
            map.put("title",title);
        }
        if(department!=null&&!department.equals("")&&department.length()!=0&&!department.trim().isEmpty()){
            map.put("department", department);
        }
        if(source!=null&&!source.equals("")&&source.length()!=0&&!source.trim().isEmpty()){
            map.put("source", source);
        }
        map.put("dictType", "appNotice");
        IPage<SubsystemNews> subsystemNewsIPage = iSubsystemNewsService.selectPortalNoticePageVoMap(page,map);
        return new ResultUtil<IPage<SubsystemNews>>().setData(subsystemNewsIPage);
    }
    @RequestMapping(value = "/addSubsystemNews", method = RequestMethod.POST)
    @ApiOperation(value = "新闻保存数据")
    public Result<Object> addSubsystemNews(@ModelAttribute SubsystemNews subsystemNews) {
        log.info("调用addSubsystemNews方法");
        subsystemNews.setCreateTime(new Date());
        subsystemNews.setUpdateTime(new Date());
        subsystemNews.setCode("1");
        boolean save = iSubsystemNewsService.save(subsystemNews);
        if(save){
            return new ResultUtil<Object>().setSuccessMsg("添加成功");
        }
        return new ResultUtil<Object>().setErrorMsg("添加失败");
    }
    @RequestMapping(value = "/addSubsystemNotice", method = RequestMethod.POST)
    @ApiOperation(value = "公告保存数据")
    public Result<Object> addSubsystemNotice(@ModelAttribute SubsystemNews subsystemNews) {
        log.info("调用addSubsystemNews方法");
        subsystemNews.setCreateTime(new Date());
        subsystemNews.setUpdateTime(new Date());
        //设置默认值
       /* subsystemNews.setSource("native");//来源本地*/
        /*subsystemNews.setInfoCode("1");//本地数据*/
        if( subsystemNews.getSource() == null || subsystemNews.getSource().length()==0 || subsystemNews.getSource().equals("") || subsystemNews.getSource().trim().isEmpty() ){
            subsystemNews.setSource(CommonConstant.SOURCE);
        }
        if( subsystemNews.getInfoCode() == null || subsystemNews.getInfoCode().length()==0 || subsystemNews.getInfoCode().equals("") || subsystemNews.getSource().trim().isEmpty() ){
            subsystemNews.setInfoCode(CommonConstant.BDinfoCode);
        }
        System.out.println("測試:"+subsystemNews.getInfoCode()+subsystemNews.getSource());

        //用来区分公告和新闻的字段
        subsystemNews.setCode("2");
        boolean save = iSubsystemNewsService.save(subsystemNews);
        if(save){
            return new ResultUtil<Object>().setSuccessMsg("添加成功");
        }
        return new ResultUtil<Object>().setErrorMsg("添加失败");
    }

}
